<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
use Auth;
use QL\QueryList;

use App\Models\Book;
use App\Models\Category as cat;

class DoubanController extends Controller
{
    
    public function getAddress()
    {
        //输入豆瓣书籍网址
        return view('admin.caiji_douban');
    }

    // 采集豆瓣网站书籍信息

    public function getjson()
    {
        //得到搜索豆瓣的isbn
        $url = trim(request::input('url'));

        if($url == '')
        {
            return '网址不能为空';
        }

        // 获得书籍信息
        $info = QueryList::get($url)->find('#info')->html();

        // return $info;

        /*** isbn ***/

        preg_match_all("/<span class=\"pl\">ISBN:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $isbn = preg_replace("/<span class=\"pl\">ISBN:<\/span> /ism", '', $content[0][0]);
            $isbn = preg_replace("/<br>/ism", '', $isbn);
        } else {
            $isbn = '';
        }

        //如果已经添加这本书了，就提示“已经收录”
        $books  = Book::where('isbn', trim($isbn))->get();
        if (!$books->isEmpty()) {
            //如果不为空，说明已经收录了本书
            return "本书已经收录过了，不需要重新添加！";
            //exit();
        } 


        // 获得书名
        $bookname = QueryList::get($url)->find('#wrapper>h1')->text();

        /*** 作者 ***/

        // 获取截取内容
        preg_match_all("/<span class=\"pl\"> 作者<\/span>.*?<\/span>/ism", $info, $content);

        // return $content;

        if(!empty($content[0]))
        {
            // 去除不需要的内容
            $zuozhe = preg_replace("/<span class=\"pl\"> 作者<\/span>:/ism", '', $content[0][0]);
            // $zuozhe = QueryList::html($zuozhe)->find('a')->text();  // 数组
            $zuozhe = QueryList::html($zuozhe)->find('a')->text();            
        } else {
            $zuozhe = '';
        }        

        /*** 出版社 ***/

        // 获取截取内容
        preg_match_all("/<span class=\"pl\">出版社:<\/span>.*?<br>/ism", $info, $content);
        // return $content;

        if(!empty($content[0]))
        {
            $chubanshe = preg_replace("/<span class=\"pl\">出版社:<\/span>/ism", '', $content[0][0]);
            $chubanshe = preg_replace("/<br>/ism", '', $chubanshe);
            $chubanshe = QueryList::html($chubanshe)->find('a')->text();
        } else {
            $chubanshe = '';
        }        

        /*** 出品方 ***/

        // 获取截取内容
        preg_match_all("/<span class=\"pl\">出品方:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $chupinfang = preg_replace("/<span class=\"pl\">出品方:<\/span>/ism", '', $content[0][0]);
        } else {
            $chupinfang = '';
        }

        /*** 副标题 ***/

        preg_match_all("/<span class=\"pl\">副标题:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $fubiaoti = preg_replace("/<span class=\"pl\">副标题:<\/span>/ism", '', $content[0][0]);
            $fubiaoti = preg_replace("/<br>/ism", '', $fubiaoti);
        } else {
            $fubiaoti = '';
        }

        /*** 原作名****/

        preg_match_all("/<span class=\"pl\">原作名:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $yuanzuoming = preg_replace("/<span class=\"pl\">原作名:<\/span>/ism", '', $content[0][0]);
            $yuanzuoming = preg_replace("/<br>/ism", '', $yuanzuoming);
        } else {
            $yuanzuoming = '';
        }        

        /*** 译者 ***/

        preg_match_all("/<span class=\"pl\"> 译者<\/span>.*?<\/span>/ism", $info, $content);
 
       if(!empty($content[0]))
        {
            $yizhe = preg_replace("/<span class=\"pl\"> 译者<\/span>:/ism", '', $content[0][0]);
            $yizhe = QueryList::html($yizhe)->find('a')->text();
        } else {
            $yizhe = '';
        }
     
        /*** 出版年 ***/

        preg_match_all("/<span class=\"pl\">出版年:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $chubannian = preg_replace("/<span class=\"pl\">出版年:<\/span>/ism", '', $content[0][0]);
            $chubannian = preg_replace("/<br>/ism", '', $chubannian);
        } else {
            $chubannian = '';
        }

        /*** 页数 ***/

        preg_match_all("/<span class=\"pl\">页数:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $yeshu = preg_replace("/<span class=\"pl\">页数:<\/span>/ism", '', $content[0][0]);
            $yeshu = preg_replace("/<br>/ism", '', $yeshu);
        } else {
            $yeshu = '';
        }

        /*** 定价 ***/

        preg_match_all("/<span class=\"pl\">定价:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $dingjia = preg_replace("/<span class=\"pl\">定价:<\/span>/ism", '', $content[0][0]);
            $dingjia = preg_replace("/<br>/ism", '', $dingjia);
        } else {
            $dingjia = '';
        }

        /*** 装帧 ***/

        preg_match_all("/<span class=\"pl\">装帧:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $zhuangzhen = preg_replace("/<span class=\"pl\">装帧:<\/span>/ism", '', $content[0][0]);
            $zhuangzhen = preg_replace("/<br>/ism", '', $zhuangzhen);
        } else {
            $zhuangzhen = '';
        }

        /*** 丛书 ***/

        preg_match_all("/<span class=\"pl\">丛书:<\/span>.*?<br>/ism", $info, $content);

        if(!empty($content[0]))
        {
            $congshu = preg_replace("/<span class=\"pl\">丛书:<\/span>/ism", '', $content[0][0]);
            $congshu = QueryList::html($congshu)->find('a')->text();
        } else {
            $congshu = '';
        }        

        // 获得书籍封面图片(小)
        $fengmian = QueryList::get($url)->find('#mainpic')->find('img')->attr('src');

        // 获得书籍封面图片(大)
        $fengmian_max = QueryList::get($url)->find('#mainpic')->find('a')->attr('href');

        // 获得图书内容介绍
        $neirong1 = QueryList::get($url)->find('.indent>.short>.intro')->html();
        
        $neirong2 = QueryList::get($url)->find('.indent>.all>.>.intro')->html();

        if($neirong2 != '')
        {
            $neirong = $neirong2;
        } else {
            $neirong = $neirong1;
        }

        // 获得作者介绍
        $zuozhejieshao = QueryList::get($url)->find('.indent>.>.intro')->html();

        // substr或指定字符串最后一次出现之后的内容
        // $number = substr($url,strripos($url,"/")+1);
        $number = substr($url, 32, 8);

        $short = '#dir_'. $number . '_short';

        $full = '#dir_'. $number . '_full';

        // 获得目录介绍
        $mulu1 = QueryList::get($url)->find($short)->html();

        $mulu2 = QueryList::get($url)->find($full);
        // 移除连接
        $mulu2->find('a')->remove();
        $mulu2 = $mulu2->html();

        if($mulu2 != '')
        {
            $mulu = $mulu2;
        } else {
            $mulu = $mulu1;
        }

        // 删除空格
        $mulu = preg_replace("/    /ism", '', $mulu);
        // 删除()
        $mulu = str_replace("()", '', $mulu);
        $mulu = str_replace("· · · · · · ", '', $mulu);

        // 图书分类名字
        $cats  = cat::all();

        //替换介绍里的/n字符为<br/>换行符号
        // $summary = mb_ereg_replace('\n', '<br/>', $summary);
        // $author_intro = mb_ereg_replace('\n', '<br/>', $author_intro);
        // $catalog = mb_ereg_replace('\n', '<br/>', $catalog);

        return view('admin.douban_addbook', array(
            'cats'=> $cats,

            'bookname' => trim($bookname), // 书籍名字
            'images'=> trim($fengmian), // 书籍封面(小图片)
            'images_max' => trim($fengmian_max), // 书籍封面(大小图片)
            'zuozhe' => trim($zuozhe),  // 作者
            'chubanshe' => trim($chubanshe), // 出版社
            'chupinfang' => trim($chupinfang), // 出品方
            'fubiaoti' => trim($fubiaoti), // 副标题
            'yuanzuoming' => trim($yuanzuoming), //原作名 
            'yizhe' => trim($yizhe), // 译者
            'chubannian' => trim($chubannian), //出版年
            'yeshu' => trim($yeshu), // 页数
            'dingjia' => trim($dingjia), // 定价
            'zhuangzhen' => trim($zhuangzhen), // 装帧
            'congshu' => trim($congshu), // 丛书        
            'isbn' => trim($isbn), // isbn

            'neirong' => $neirong, //内容简介
            'zuozhejieshao' => $zuozhejieshao, //作者简介
            'mulu' => $mulu //目录简介
            ));

    }    
}
